
#include <BAction>
#include "member_BAction.h"
#include "member_BShortcut.h"

using namespace BWE;

#define member					(*(member_BAction*)_ptr)
#define member_allocate()		_ptr = new member_BAction(this)
#define member_release()		delete (member_BAction*)_ptr

BAction::BAction()
{
	member_allocate();
}
BAction::BAction(const BString& text)
{
	member_allocate();
	setText(text);
}
BAction::BAction(BIcon* icon, const BString& text)
{
	member_allocate();
	setIcon(icon);
	setText(text);
}
BAction::BAction(BImage* image, const BString& text)
{
	member_allocate();
	setImage(image);
	setText(text);
}
BAction::~BAction()
{
	if (member.shortcut)
		member.shortcut->setHost(0);
	member_release();
}

void BAction::setVisible(bool visible)
{
	if (member.visible != visible)
	{
		member.visible = visible;
		if (member.visible)
			this->emit(Signal_Show, true);
		else
			this->emit(Signal_Hide, false);
	}
}
bool BAction::visible() const
{
	return member.visible;
}

void BAction::setColor(BByte r, BByte g, BByte b, BByte a)
{
	setColor(BColor(r, g, b, a));
}
void BAction::setColor(const BColor& color)
{
	if (member.color != color)
	{
		member.color = color;
		if (member.host)
			member.host->fresh();
	}
}
const BColor& BAction::color() const
{
	return member.color;
}

void BAction::setGraph(Graph graph)
{
	if (member.graph != graph)
	{
		member.graph = graph;
		emit(Signal_GraphChanged, member.graph);
		if (member.host)
			member.host->fresh();
	}
}
Graph BAction::graph() const
{
	return member.graph;
}

void BAction::setImage(BImage* image)
{
	if (member.image != image)
	{
		member.image = image;
		emit(Signal_ImageChanged, member.icon);
		if (member.host)
			member.host->fresh();
	}

}
const BImage* BAction::image() const
{
	return member.image;
}
BImage* BAction::image()
{
	return member.image;
}

void BAction::setIcon(BIcon* icon)
{
	if (member.icon != icon)
	{
		member.icon = icon;
		emit(Signal_IconChanged, member.icon);
		if (member.host)
			member.host->fresh();
	}
}
BIcon* BAction::icon()
{
	return member.icon;
}
const BIcon* BAction::icon() const
{
	return member.icon;
}

void BAction::setText(const BString& text)
{
	if (member.text != text)
	{
		member.text = text;
		emit(Signal_TextChanged, member.text);
		if (member.host)
			member.host->fresh();
	}
}
const BString& BAction::text() const
{
	return member.text;
}

void BAction::setTips(const BString& tips)
{
	if (member.tips != tips)
	{
		member.tips = tips;
		emit(Signal_TipsChanged, member.graph);
	}
}
const BString& BAction::tips() const
{
	return member.tips;
}

bool BAction::setShortcut(const BString& text, BWidget* widget)
{
	if (member.shortcut.empty())
	{
		member.shortcut = new BShortcut();
		member.shortcut->setHost(this);
	}
	member.shortcut->setWidget(widget);
	return member.shortcut->setText(text);
}
bool BAction::setShortcut(const BKeyArray& keys, BWidget* widget)
{
	if (member.shortcut.empty())
	{
		member.shortcut = new BShortcut();
		member.shortcut->setHost(this);
	}
	member.shortcut->setWidget(widget);
	return member.shortcut->setKeys(keys);
}

bool BAction::setShortcut(BShortcut* shortcut)
{
	if (member.shortcut != shortcut)
	{
		if (BShortcut* scut = member.shortcut)
		{
			member.shortcut->setHost(0);
		}
		member.shortcut = shortcut;
		if (BShortcut* scut = member.shortcut)
		{
			member.shortcut->setHost(this);
		}
		return true;
	}
	return false;
}
BShortcut* BAction::shortcut()
{
	return member.shortcut;
}

void BAction::setCheckable(bool checkable)
{
	if (member.checkable != checkable)
	{
		if (member.checkable && member.checked)
		{
			member.checked = false;
			emit(Signal_Checked, false);
		}
		member.checkable = checkable;
		if (member.host)
			member.host->fresh();
	}
}
bool BAction::checkable() const
{
	return member.checkable;
}

void BAction::setChecked(bool checked)
{
	if (member.checkable && member.checked != checked)
	{
		member.checked = checked;
		if (member.host)
			member.host->fresh();
		emit(Signal_Checked, member.checked);
	}
}
bool BAction::checked() const
{
	return member.checked;
}

void BAction::trigger()
{
	if (this->enabled())
	{
		if (member.checkable)
		{
			member.checked = !member.checked;
			emit(Signal_Checked, member.checked);
		}
		emit(Signal_Triggered, member.checked);
	}
}
